package com.sign2024.sign2024project.capitalconstruction.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.ExcelStyleUtils;
import com.sign2024.sign2024core.entry.capitalconstruction.JjMajorCollect;
import com.sign2024.sign2024project.capitalconstruction.param.JjMajorParam;
import com.sign2024.sign2024project.capitalconstruction.service.JjMajorCollectService;
import com.sign2024.sign2024project.projecttask.service.ProjectTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 基建专业汇总表  控制层
 *
 * @author yujie
 * @data 2024/8/24 11:58
 */
@Api(tags = "基建")
@RestController
@RequestMapping("/api/jjMajor")
public class JjMajorCollectController {

    @Resource
    private JjMajorCollectService jjMajorCollectService;

    @Resource
    private ProjectTaskService projectTaskService;

    @Log(title = "基建", businessType = "导入")
    @ApiOperation(value = "基建专业汇总表excel导入", notes = "基建专业汇总表excel导入")
    @Transactional
    @PostMapping("/import")
    public CommonResult<JjMajorCollect> importInfo(MultipartFile file) {
//    public CommonResult<JjMajorCollect> importInfo(MultipartFile file, Integer taskId) {
//        if (StringUtils.isNull(taskId)) {
//            throw new CommonException(CommonConstants.ERROR, "任务标识为空");
//        }

//        // 删除该任务下的数据
//        jjMajorCollectService.deleteByTaskId(taskId);
//        // 将 任务 id 进行赋值
//        JjMajorCollectExcelListener listener = new JjMajorCollectExcelListener();
//        listener.setTaskId(taskId);
        try {

            // 获取文件名
            // 获取上传文件的名称
            String fileName = file.getOriginalFilename();

//            EasyExcel.read(file.getInputStream(), listener).head(JjMajorCollect.class).sheet().doReadSync();
            List<JjMajorCollect> list = EasyExcel.read(file.getInputStream()).head(JjMajorCollect.class).sheet().doReadSync();

            jjMajorCollectService.exportJjMajorCollect(list, fileName);


//            // 导入数据后， 将任务中的 基建专业汇总表 列修改为 已导入
//            ProjectTask projectTask = new ProjectTask();
//            projectTask.setId(taskId);
//            projectTask.setBatchNeed("已导入");
//            projectTaskService.updateTask(projectTask);

            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @Log(title = "基建", businessType = "查询")
    @ApiOperation(value = "分页查询基建专业汇总", notes = "分页查询基建专业汇总")
    @PostMapping("/page")
    public CommonResult<JjMajorCollect> page(JjMajorParam param) {
        Page<JjMajorCollect> page = jjMajorCollectService.pageJjMajor(param);
        return CommonResult.success(page);
    }

    @Log(title = "基建", businessType = "更新")
    @ApiOperation(value = "修改基建专业汇总", notes = "修改基建专业汇总")
    @PostMapping("/update")
    public CommonResult<JjMajorCollect> update(JjMajorCollect param) {
        return CommonResult.success(jjMajorCollectService.updateJjMajorCollect(param));
    }

    @Log(title = "基建", businessType = "其他")
    @ApiOperation(value = "基建标段对应关系数据历史回退", notes = "基建标段对应关系数据历史回退")
    @Transactional
    @PostMapping("/rollback")
    public CommonResult rollback() {
        jjMajorCollectService.rollbackHistorical();
        return CommonResult.success();
    }

    @Log(title = "基建", businessType = "导出")
    @ApiOperation(value = "基建专业汇总excel导出", notes = "基建专业汇总excel导出")
    @PostMapping("/export")
    public void exportInfo(HttpServletResponse response) {
        List<JjMajorCollect> list = jjMajorCollectService.selectListAll();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = null;
        try {
            fileName = URLEncoder.encode("基建专业汇总", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();

            HorizontalCellStyleStrategy styleBorder =
                    ExcelStyleUtils.createStyleBorder((short) 10, "宋体", (short) 10, "宋体");

            EasyExcel.write(outputStream, JjMajorCollect.class)
                    .registerWriteHandler(styleBorder)
                    .sheet("基建专业汇总").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "基建专业汇总导出失败");
        }
    }
}
